package com.kust.smarthome.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kust.smarthome.web.mapper.DeviceStateMapper;
import com.kust.smarthome.web.model.DeviceState;
import com.kust.smarthome.web.service.IDeviceStateService;
import com.kust.smarthome.web.vo.DeviceStateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class DeviceStateImpl extends ServiceImpl<DeviceStateMapper, DeviceState> implements IDeviceStateService {

    @Autowired
    private DeviceStateMapper deviceStateMapper;

    @Override
    public void insert(DeviceState deviceState) {
        deviceStateMapper.insert(deviceState);
    }

    @Override
    public DeviceState getState(Integer deviceId, Integer typeId) {
        QueryWrapper<DeviceState> query = new QueryWrapper<>();
        query.eq("device_id", deviceId)
                .eq("type_id", typeId);
        return deviceStateMapper.selectOne(query);
    }

    @Override
    public int updateDevice(DeviceStateVo deviceStateVo) {
        DeviceState deviceState = new DeviceState()
                .setIsUpdate(0)
                .setDeviceId(deviceStateVo.getDeviceId())
                .setTypeId(deviceStateVo.getTypeId())
                .setOperation(deviceStateVo.getOperation());

           int num = deviceStateMapper.insert(deviceState);
           System.out.println(deviceState);
          return deviceState.getId();
    }

    @Override
    public List<DeviceState> findOperationList(Integer deviceId, Integer typeId) {
        return deviceStateMapper.getOperationList(deviceId,typeId);

    }

    @Override
    public boolean setupdate(Integer id) {

        return deviceStateMapper.changeUpdate(id);
    }

}
